(function ($) {
	var wardID = ''
	var TomcatUrl = JSON.parse(localStorage.getItem('$user')).TomcatUrl;
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function getListData(){
		mui('#otherScoll').off('tap','.mui-icon-phone',dialPhone)
		mui('#otherScoll').off('tap','.groupName',changeGroup)
		mui('#otherScoll .photo img').each(function (index,element) {
			element.removeEventListener('error',photoLoadError)
		})
		$('#otherWrapper').scroll().scrollTo(0,0,100);
		var pdict = {'ward':wardID,"start":0,"limit":'','input':$('#search')[0].value}
		var paramDict = {'method':'web.INMHRComm:FindNotWardNurseList','params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result.msgcode != '1') {
				showNullTip(result.msg)
				return
			}
			updateUI(result['data']);
		}, function(errorStr) {
			showNullTip(errorStr)
		});
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		wardID = self.wardID
		mui("body").on("tap", ".searchArea .mui-icon-clear", function() {
			if (document.getElementById('search').value != "") {
				document.getElementById('search').value = ""
			}
			getListData()
		});
		var search = document.getElementById('search');
		search.addEventListener('input',function(){
			getListData()
		});
		getListData()
	}
	function updateUI(dataArr){
		var innerHtml = ''
		var wardCount = 0
		var angleStr = '<div class="angleIcon"><img src="../dependence/user/images/angledown.svg"></div>'
		var liHtmls = "",wardDesc = ""
		for (var i = 0; i < dataArr.length;i++) {
			var dict = dataArr[i]
			if (wardDesc != dict['WardDesc']) {
				if (i == 0) {
					wardDesc = dict['WardDesc']
				}else{
					var active = ''
					if (innerHtml == '') {
						active = 'mui-active'
					}
					innerHtml += '<div class="wardGroup '+active+'">'+
						'<div class="groupName">'+angleStr+ wardDesc + '<span>（'+wardCount+'）人</span></div>'+
						'<ul class="mui-table-view">' +liHtmls + '</ul></div>'
					wardCount = 0
					wardDesc = dict['WardDesc']
					liHtmls = ''
				}
			}
			wardCount += 1
			var name = dict['PerName']
			var photo = dict['PerPhoto']||""
			name = name.split('(')[0]
			if (name.length > 2) {
				name = name.substring(name.length-2,name.length)
			}
			var phone = dict['PerPhone']||""
			phone = (phone==""?'暂无':phone)
			var isHead = dict['IsHead']=="Y"?'<div class="nurHead">护士长</div>':''
			liHtmls += '<li class="mui-table-view-cell">' +
				'<div class="photo" data-name="'+name+'" '+(photo==""?"":'style="background-color: transparent;border: solid 1px #75AC47;"')+'>' + 
					(photo==""?name:('<img src="'+TomcatUrl + '/Fileupdate/' + photo +'">')) + '</div>' +
				'<div class="nurName">' +
					'<div class="name"><div>' + dict['PerName'] + '</div>' + isHead + '</div>'+
					'<div class="phone">' + phone + ' </div>' +
				'</div>' +
				'<div class="mui-icon mui-icon-phone"></div></li>'
		}
		
		if (dataArr.length == 0) {
			showNullTip('暂无数据')
			$('#otherScoll')[0].innerHTML = ''
		}else{
			hideNullTip()
			innerHtml += '<div class="wardGroup">'+
				'<div class="groupName">'+angleStr+ wardDesc + '<span>（'+wardCount+'）人</span></div>'+
				'<ul class="mui-table-view">' +liHtmls + '</ul></div>'
			$('#otherScoll')[0].innerHTML = innerHtml
			mui('#otherScoll').on('tap','.mui-icon-phone',dialPhone)
			mui('#otherScoll').on('tap','.groupName',changeGroup)
			mui('#otherScoll .photo img').each(function (index,element) {
				element.addEventListener('error',photoLoadError)
			})
		}
	}
	function dialPhone(){
		var phoneStr = this.parentNode.querySelector('.phone').innerText
		if (phoneStr == '暂无') {
			mui.toast('无电话号码')
			return
		}else if (phoneStr.length < 5) {
			mui.toast('号码不正确')
			return
		}
		plus.device.dial(phoneStr, true);
	}
	function changeGroup(){
		var oldActive = $('#otherScoll .wardGroup.mui-active')[0];
		var parentNode = this.parentNode
		if (oldActive) {
			oldActive.classList.remove('mui-active')
			if (parentNode == oldActive) {
				return
			}
		}
		parentNode.classList.add('mui-active')
		mui('#otherWrapper').scroll().reLayout();
		var scollH = $('#otherScoll')[0].clientHeight
		var wrapperH = $('#otherWrapper')[0].clientHeight
		if (scollH <= wrapperH) {
			return
		}
		
		//病区名 滚动到顶部
		var domTop = getElementAbsPosition(parentNode)
		if (scollH - domTop > wrapperH) {
			$('#otherWrapper').scroll().scrollTo(0,-domTop,100);
		}
	}
	function photoLoadError(){
		var photoDom = this.parentNode
		photoDom.innerHTML = photoDom.getAttribute('data-name')
		photoDom.removeAttribute('style')
	}
	function showNullTip(msg){
		mui('.nullData')[0].style.display = 'block';
		mui('.nullTip')[0].innerHTML = msg
	}
	
	function hideNullTip(){
		mui('.nullData')[0].style.display = 'none';
	}
	
	function getElementAbsPosition(item) {
		var actualTop = item.offsetTop;
		var current = item.offsetParent;
		while(current) {
			actualTop += (current.offsetTop + current.clientTop);
			current = current.offsetParent;
			if (current.id == 'otherWrapper') {
				break
			}
			//console.log(current.getAttribute('class'))
		}
		return actualTop
	}
})(mui)